package org.training.apr.dao.fake;

import java.util.Hashtable;
import java.util.Map;
import java.util.TreeMap;


public abstract class NonGenericFakeDao {
	private static final Map<Class<?>, TreeMap<Long, Object>> maps =  
		                new Hashtable<Class<?>, TreeMap<Long, Object>>();
	
	private TreeMap<Long, Object> getMap(Class<?> aClass) {
		TreeMap<Long, Object> map = maps.get(aClass);
		if(map == null) {
			throw new FakeDaoException("The map for class" + aClass.getName() + " dosen't exist");
		}
		return map;
	}

	public NonGenericFakeDao(Class<?> aClass) {
		maps.put(aClass, new TreeMap<Long, Object>());
	}
	
	public void save(Identifiable instance) {
		getMap(instance.getClass()).put(instance.getId(), instance);
	}
	
	public Object read(Class<?> aClass, Long id) {
		return getMap(aClass).get(id);
	}
	
	public void delete(Class<?> aClass, Identifiable instance) {
		getMap(aClass).remove(instance.getId());
	}
}